﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 系统未捕获的异常处理方式
{

    public class TestRaiseException
    {
        ~TestRaiseException()
        {
            int i = 0;
            int j = 1 / i;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
            TestRaiseException testRaiseException = new TestRaiseException();
        }

        static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                using (FileStream fs = new FileStream(@"c:\testme.log", FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter w = new StreamWriter(fs, System.Text.Encoding.UTF8))
                    {
                        w.WriteLine(e.ExceptionObject);
                    }
                }
            }
            catch
            {
            }
        }
    }
}
